﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Interface;
using Controller;
using System.IO;
using Microsoft.Win32;

namespace GUI
{
    /// <summary>
    /// Interaction logic for Konkurrencer.xaml
    /// </summary>
    public partial class KonkurrenceVindue : Window
    {
        #region Fields
        
        MainController controller;
        IMesse aktuelMesse;
        IKonkurrence aktuelKonkurrence;
        KategoriVindue seKategorier; //xaml vinduet kategorier

        #endregion

        #region Properties

        public IKonkurrence AktuelKonkurrence
        {
            get { return aktuelKonkurrence; }
            set { aktuelKonkurrence = value; }  
        }

        #endregion

        #region Creation

        public KonkurrenceVindue(IMesse aktuelMesse, MainController controller)
        {
            InitializeComponent();
            UpdateListView();
            this.DataContext = this;

            this.aktuelMesse = aktuelMesse;
            this.controller = controller;
        }

        #endregion

        #region Events

        private void btn_tilbage_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn_Indlæs_Click(object sender, RoutedEventArgs e)
        {
            string path = "";
            string fileName = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "CSV Fil (.csv)|*.csv";
            ofd.FilterIndex = 0;
            ofd.Multiselect = false;
            ofd.ShowDialog();

            try
            {
                path = System.IO.Path.GetDirectoryName(ofd.FileName);
                fileName = System.IO.Path.GetFileName(ofd.FileName);
                controller.IndlæsCSV(path + "\\" + fileName, aktuelMesse);
            }
            catch (Exception)
            {

            }
    
            UpdateListView();
        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            aktuelKonkurrence = (IKonkurrence)listView1.SelectedItem;
        }

        private void btn_Kategorier_Click(object sender, RoutedEventArgs e)
        {
            seKategorier = new KategoriVindue(controller, aktuelKonkurrence);
            seKategorier.ShowDialog();
        }

        private void btn_Teams_Click(object sender, RoutedEventArgs e)
        {
            TeamVindue teamVindue = new TeamVindue();
            teamVindue.ShowDialog();
        }

        #endregion
  
        #region Private Helpers

        private void UpdateListView()
        {
            try
            {
                foreach (IKonkurrence konkurrence in controller.HentKonkurrencer(aktuelMesse))
                {
                    listView1.Items.Add(konkurrence);
                }
            }
            catch
            {

            }
            
        }

        #endregion
    }
}
